<?php
class Account extends MY_Controller{
	
	function __construct(){
		
		parent::__construct();
		if(!$this->member) redirect('login/account');
		
	}
	
	function index()
	{
		$data['title'] = $this->title('Compte');
		$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
		$order = order::find('all',array('limit'=>1,'order'=>'order_time desc','conditions'=>array('member_id'=>$this->member->id)));
		if($order){
		$data['order'] = $order;
		
		$data['orderitem'] = orderitem::find('all',array('conditions'=>array('order_num'=>$order[0]->order_num),'order'=>'product_id asc'));
		if($order[0]->address_id !=NULL){
		$data['address'] = adresse::find_by_id($order[0]->address_id);
		}
		}else{
		$data['order']=NULL;
		}
		$data['sauces'] = product::find('all',array('conditions'=>array('cat_id'=>16,'status'=>! null)));
		$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
		$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
		$this->load->view('header',$data);
	    $this->load->view('account/index',$data);
	    $this->load->view('footer',$data);
	}
	
	
	function update_password()
	{
		$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
	if($_POST)
		{
			$member = $this->member;
			
			$this->form_validation->set_rules('password', 'Password', 'required|matches[passconf]');
			$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
		if ($this->form_validation->run() == FALSE)
							{
								$data['message']= "Vérifiez votre mot de passe !";
							}else{
								$set_newpassword= member::set_newpwd($member->id,$_POST['password'],$member->email);
								if($set_newpassword == true){
									
								$data['message']= "Votre nouveau mot de passe à été bien enregistré.";	
									
								}
							}
		
		$data['title'] = $this->title('Compte');
		$data['sauces'] = product::find('all',array('conditions'=>array('cat_id'=>16,'status'=>! null)));
		$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
		$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
		$this->load->view('header',$data);
	    $this->load->view('account/password',$data);
	    $this->load->view('footer',$data);	
			
		}else{
		
		$data['title'] = $this->title('Compte');
		$data['sauces'] = product::find('all',array('conditions'=>array('cat_id'=>16,'status'=>! null)));
		$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
		$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
		$this->load->view('header',$data);
	    $this->load->view('account/password',$data);
	    $this->load->view('footer',$data);	
		
		}
		
	}

	function address()
	{
		$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
		if($_POST){
			$com = community::find_by_id($_POST['com']);
			$delivery_address = adresse::find_by_member_id($this->member->id);
			if($delivery_address){
				$delivery_address->ring = $_POST['ring'];
				$delivery_address->postal = $com->code;
				$delivery_address->community = $com->name;
				$delivery_address->house_num = $_POST['house_num'];
				$delivery_address->address = $_POST['address'];
				$delivery_address->tele = $_POST['tele'];
				if($_POST['company']==''){
				$delivery_address->company = null;					
				}else{
				$delivery_address->company = $_POST['company'];	
				}
				$delivery_address->save();
			}else{
				if($_POST['company']==''){
				$company = null;					
				}else{
				$company = $_POST['company'];	
				}
				$address_data = array('id'=>'','member_id'=>$this->member->id,'ring'=>$_POST['ring'],'postal'=>$com->code,'community'=>$com->name,'house_num'=>$_POST['house_num'],'address'=>$_POST['address'],'tele'=>$_POST['tele'],'company'=>$company);
				$delivery_address = adresse::create($address_data);
				$delivery_address->save();
			}
			
			
			$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
			$data['title'] = $this->title('Compte');
			$data['coms'] = community::find('all');
			$data['msg'] = 'Votre adresse de livraison a été mis à jour.';
			$data['delivery_adresse'] = $delivery_address;
			$data['sauces'] = product::find('all',array('conditions'=>array('cat_id'=>16,'status'=>! null)));
			$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
			$this->load->view('header',$data);
			$this->load->view('account/address',$data);
			$this->load->view('footer',$data);
		}else{
			$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
			$data['title'] = $this->title('Compte');
			$data['coms'] = community::find('all');
			$data['delivery_adresse'] = adresse::find_by_member_id($this->member->id);
			$data['sauces'] = product::find('all',array('conditions'=>array('cat_id'=>16,'status'=>! null)));
			$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
			$this->load->view('header',$data);
			$this->load->view('account/address',$data);
			$this->load->view('footer',$data);
		}
	}

	function edit_address()
	{
		$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
		$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
		if($_POST){
			$this->form_validation->set_rules('ring','sonnette', 'required');
			$this->form_validation->set_rules('house_num','numéro', 'required');
			$this->form_validation->set_rules('address','adresse', 'required');
			$this->form_validation->set_rules('tele','téléphone', 'numeric|required');
			if($this->form_validation->run()==FALSE){

		
		$data['title'] = $this->title('Compte');
		$data['communities'] = community::get_communities();
		$data['delivery_adresse'] = adresse::find_by_id($this->uri->segment(3));
		$data['sauces'] = product::find('all',array('conditions'=>array('cat_id'=>16,'status'=>! null)));
		$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
		$this->load->view('header',$data);
	    $this->load->view('account/edit_address',$data);
	    $this->load->view('footer',$data);	
			}else{
		
			$updated_address = adresse::update_adresse($_POST);
			if($updated_address==true){
				redirect('account/address');
			}
			}
		}else{
	
		$data['title'] = $this->title('Compte');
		$data['communities'] = community::get_communities();
		$data['delivery_adresse'] = adresse::find_by_id($this->uri->segment(3));
		$data['sauces'] = product::find('all',array('conditions'=>array('cat_id'=>16,'status'=>! null)));
		$data['files'] = get_dir_file_info('lunch_pdf', $top_level_only = TRUE);
		$this->load->view('header',$data);
	    $this->load->view('account/edit_address',$data);
	    $this->load->view('footer',$data);	
	
		}
	}
	
	function new_address()
	{
		$data['shop'] = shop::find('all',array('conditions'=>array('status','1')));
		$data['menu'] = cat::find('all',array('conditions'=>array('parent_id'=>NULL)));
		if($_POST){
			$this->form_validation->set_rules('community_id','Commune','required|is_natural');
			$this->form_validation->set_rules('ring','sonnette', 'required');
			$this->form_validation->set_rules('house_num','numéro', 'required');
			$this->form_validation->set_rules('address','adresse', 'required');
			$this->form_validation->set_rules('tele','téléphone', 'numeric|required');
			if($this->form_validation->run()==FALSE){
					if(isset($this->member)) $data['member'] = $this->member;
					$data['title'] = 'sushibox | Votre Compte';
					$data['communities'] = community::get_communities();
					$this->load->view('header',$data);
	   				$this->load->view('account/new_address',$data);
	    			$this->load->view('footer',$data);	
			}else{
				$new_address = adresse::insert_address($_POST);
				
				if($new_address == true){
					if($_GET['mode']==1){
						redirect('cart');
					}elseif($_GET['mode']==2){
						redirect('cart/reservation');
					}elseif($_GET['mode']==''){
						redirect('account/address');
					}
				}else{
						echo 'false';
				}
			}
		}else{
		if(isset($this->member)) $data['member'] = $this->member;
		$data['title'] = $this->title('Compte');
		$data['communities'] = community::get_communities();
		$this->load->view('header',$data);
	    $this->load->view('account/new_address',$data);
	    $this->load->view('footer',$data);	
		}
	}
	
	function delete_address()
	{
		if($_GET){
			$delete_item = adresse::find_by_id($_GET['id']);
			$delete_item->delete();
				echo json_encode('ok');
		}
	}
}